/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unisc.pdaapp.xml;

import br.unisc.pdaapp.meta.Course;
import br.unisc.pdaapp.meta.Lesson;
import br.unisc.pdaapp.meta.Material;
import br.unisc.pdaapp.meta.Message;
import br.unisc.pdaapp.meta.Student;
import br.unisc.pdaapp.meta.Tutor;
import br.unisc.pdaapp.meta.User;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author guilherme
 */
public class MoodleXmlReader
{

    public static final int COURSE = 0;

    public static final int CLASSES = 1;

    public static final int MESSAGES = 2;

    public static final int MATERIAL = 3;

    public static final int USERS = 4;

    private Vector resultData = null;

    public MoodleXmlReader()
    {
        super();
    }

    public Vector getResultData()
    {
        return resultData;
    }

    public void readXML(String xml, int xmlTypeToRead, byte[] byteArrayStream) throws Exception
    {
        //Initilialize XML parser
        KXmlParser parser = new KXmlParser();
        resultData = new Vector();

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayStream);
        parser.setInput(new InputStreamReader(byteArrayInputStream));

        parser.nextTag();

        parser.require(XmlPullParser.START_TAG, null, "root");

        //Iterate through our XML file
        while (parser.nextTag() != XmlPullParser.END_TAG)
        {
            switch (xmlTypeToRead)
            {
                case COURSE:
                    readXMLCourses(parser);
                    break;

                case CLASSES:
                    readXMLClasses(parser);
                    break;

                case MESSAGES:
                    readXMLMessages(parser);
                    break;

                case MATERIAL:
                    readXMLMaterial(parser);
                    break;

                case USERS:
                    readXMLUsers(parser);
                    break;

                default:
                    break;
            }
        }

        parser.require(XmlPullParser.END_TAG, null, "root");

        parser.next();

        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    }

    private void readXMLMaterial(KXmlParser parser) throws XmlPullParserException, IOException
    {
        Material material = new Material();

        parser.require(XmlPullParser.START_TAG, null, "material");

        while (parser.nextTag() != XmlPullParser.END_TAG)
        {
            parser.require(XmlPullParser.START_TAG, null, null);

            String name = parser.getName();
            String text = parser.nextText();

            if (name.equals("inst"))
            {
                material.setId(Integer.parseInt(text));
            }
            else if (name.equals("cmid"))
            {
                material.setCourseModuleID(Integer.parseInt(text));
            }
            else if (name.equals("mid"))
            {
                material.setModuleID(Integer.parseInt(text));
            }
            else if (name.equals("itname"))
            {
                material.setName(text);
            }
            else if (name.equals("itdesc"))
            {
                material.setDesc(text);
            }
            else if (name.equals("vis"))
            {
                material.setVisible(text.equals("1"));
            }

            parser.require(XmlPullParser.END_TAG, null, name);
        }

        resultData.add(material);

        parser.require(XmlPullParser.END_TAG, null, "material");
    }

    private void readXMLCourses(KXmlParser parser) throws XmlPullParserException, IOException
    {
        Course course = new Course();

        parser.require(XmlPullParser.START_TAG, null, "course");

        while (parser.nextTag() != XmlPullParser.END_TAG)
        {

            parser.require(XmlPullParser.START_TAG, null, null);

            String name = parser.getName();
            String text = parser.nextText();

            if (name.equals("cid"))
            {
                course.setId(text);
            }
            else if (name.equals("cname"))
            {
                course.setName(text);
            }

            parser.require(XmlPullParser.END_TAG, null, name);
        }

        resultData.add(course);

        parser.require(XmlPullParser.END_TAG, null, "course");
    }

    private void readXMLClasses(KXmlParser parser) throws XmlPullParserException, IOException
    {
        Lesson lesson = new Lesson();

        parser.require(XmlPullParser.START_TAG, null, "csection");

        while (parser.nextTag() != XmlPullParser.END_TAG)
        {

            parser.require(XmlPullParser.START_TAG, null, null);

            String name = parser.getName();
            String text = parser.nextText();

            if (name.equals("sid"))
            {
                lesson.setClassID(Integer.parseInt(text));
            }
            else if (name.equals("sec"))
            {
                lesson.setSection(Integer.parseInt(text));
            }
            else if (name.equals("vis"))
            {
                lesson.setVisible(text.equals("1"));
            }
            else if (name.equals("sum"))
            {
                lesson.setSummary(text);
            }

            parser.require(XmlPullParser.END_TAG, null, name);
        }

        resultData.add(lesson);

        parser.require(XmlPullParser.END_TAG, null, "csection");
    }

    private void readXMLMessages(KXmlParser parser) throws XmlPullParserException, IOException
    {
        Message message = new Message();

        parser.require(XmlPullParser.START_TAG, null, "msg");

        while (parser.nextTag() != XmlPullParser.END_TAG)
        {
            parser.require(XmlPullParser.START_TAG, null, null);

            String name = parser.getName();
            String text = parser.nextText();

            if (name.equals("mid"))
            {
                message.setId(Integer.parseInt(text));
            }
            else if (name.equals("from"))
            {
                message.setUserFrom(text);
            }
            else if (name.equals("msg_val"))
            {
                message.setMessage(text);
            }

            parser.require(XmlPullParser.END_TAG, null, name);
        }

        resultData.add(message);

        parser.require(XmlPullParser.END_TAG, null, "msg");
    }

    private void readXMLUsers(KXmlParser parser) throws XmlPullParserException, IOException
    {
        User user = null;

        parser.require(XmlPullParser.START_TAG, null, "user");

        String id = null;
        String userName = null;
        boolean isStudent = true;

        while (parser.nextTag() != XmlPullParser.END_TAG)
        {
            parser.require(XmlPullParser.START_TAG, null, null);

            String name = parser.getName();
            String text = parser.nextText();

            if (name.equals("isStudent"))
            {
                isStudent = text.equals("1");
            }

            if (name.equals("name"))
            {
                userName = text.toString();
            }

            parser.require(XmlPullParser.END_TAG, null, name);
        }

        if (isStudent)
        {
            user = new Student();
        }
        else
        {
            user = new Tutor();
        }

        user.setUserName(userName);

        resultData.add(user);

        parser.require(XmlPullParser.END_TAG, null, "user");
    }
}
